package org.lw.bootstrap.controller;

import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

/**
 * @author LiuWu
 *
 */
public class CglibProxy implements MethodInterceptor {
	private Object target;

	private Logger logger = null;

	/**
	 * 创建代理对象
	 * @param target
	 * @return
	 */
	public Object getInstance(Object target) {
		this.target = target;
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(this.target.getClass());
		enhancer.setCallback(this);
		logger = LoggerFactory.getLogger(target.getClass());
		return enhancer.create();
	}

	@Override
	public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
		Object result = null;
		logger.info("Method Name: {} Start", method.getName());
		result = methodProxy.invokeSuper(proxy, args);
		logger.info("Method Name: {} End", method.getName());
		return result;
	}

}
